<?php


namespace api\zmt\controller;


use api\zmt\model\AdminModel;
use api\zmt\validate\AdminValidate;
use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\facade\Filesystem;
use think\facade\Request;


class AdminController extends RestBaseController
{
    public function register()
    {
        //接收数据
        $data = input('post.');
        //查重
        $repeat = AdminModel::where('mobile','=',$data['mobile'])->find();
        if($repeat){
            $this->error('手机号已注册');
        }

        //验证器
        $validate = new AdminValidate();
        $res = $validate->scene('register')->check($data);
        if(!$res){
            $this->error($validate->getError());
        }
        //检验验证码
        $check = checkSmsCode($data['mobile'],$data['code']);
        if(!$check){
            $this->error('验证码错误');
        }

        //入库
        if(AdminModel::create($data)){
            $this->success('注册成功');
        }else{
            $this->error('注册失败');
        }
    }

    //发送验证码
    public function sendSmsCode()
    {
        $mobile = input('mobile');
        //验证码
        $code = sendAliSms($mobile);
        if($code['code'] == 1){
            $this->success('发送成功');
        }else{
            $this->error($code['msg']);
        }
    }

    //登录
    public function login(){
        $data = input('post.');
        //验证器
        $validate = new AdminValidate();
        $res = $validate->scene('login')->check($data);
        if(!$res){
            $this->error($validate->getError());
        }
        //查看用户是否存在
        $user = AdminModel::where('mobile','=',$data['mobile'])->find();
        if(!$user){
            $this->error('该用户不存在');
        }
        $this->success('登陆成功',[
            //JWT
            'token' => signToken($user['id'],$user['mobile']),
        ]);

    }

    public function AdminList()
    {
        $header = request()->header();
        if(!isset($header['token']) || empty($header['token'])) {
            $this->error('缺少token !');
        }
        $key = new Key(config('app.jwt_key'),'HS256');
        try {
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception) {
            $this->error($exception->getMessage());
        }
        //halt($res->data->id);
        $userInfo = AdminModel::field('id,name,image,mobile')
            ->where('id',$res->data->id)
            ->find();
        $this->success('用户信息',$userInfo);
    }


    //修改个人信息
    public function AdminEdit()
    {
        $header = request()->header();
        if(!isset($header['token']) || empty($header['token'])) {
            $this->error('缺少token !');
        }
        $key = new Key(config('app.jwt_key'),'HS256');
        try {
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception) {
            $this->error($exception->getMessage());
        }
        $data = input('post.');
        //验证
        $user = AdminModel::find($res->data->id  );
        $user->image = $data['image'];
        $user->name = $data['name'];
        $user->mobile = $data['mobile'];
        if($user->save()) {
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }



}